package com.edushare.common.vo;

import java.io.File;
import java.util.HashMap;
import java.util.List;

import com.edushare.common.backup.SystemBackupUtils;
import com.edushare.common.util.SystemUtils;
import com.edushare.common.util.URLUtils;
import com.edushare.common.vo.auto._SystemBackup;

public class SystemBackup extends _SystemBackup {
	
	private static final long serialVersionUID = -8840615397057757553L;
	
	//Top most directory that contains all the backup directories is BackupFiles.BACKUP_ROOT_DIR_NAME
	public static final String BACKUP_DIR_PREFIX 					= "bkp-";
	public static final String DB_BKP_FILE_PREFIX 					= "database.sql";
	public static final String MEDIA_BKP_FILE_PREFIX 				= "media";
	public static final String CONFIG_FILE_NAME						= "config.xml";
//	public static final String RESTORE_DIR_PREFIX 					= "restore-";
	public static final String RESTORE_SCRIPT_FILE_NAME 			= "restore.sh";

	//Backup types
	public static final int BACKUP_TYPE_DB_ONLY 					= 10;
	public static final int BACKUP_TYPE_MEDIA_ONLY 					= 20;
	public static final int BACKUP_TYPE_COMPLETE 					= 30;
	
	//status map
	public static final int PROCESS_FINISHED_SUCCESSFULLY			= -1;
	public static final int PROCESS_PROCESSING						= 00;
	public static final int PROCESS_IN_QUEUE						= +1;

	public static final Integer IS_RESTORE_TRUE 					= 10;
	public static final Integer IS_RESTORE_FALSE 					= 0;

	public HashMap<String, String> getBackupFileURLsMap(){
		File f = new File(SystemUtils.getAbsolutePathFromRelative(getDirectoryPath()));
		HashMap<String,String> hm = new HashMap<String,String>();
		
		for(String s: f.list()){
			hm.put(s, URLUtils.getBackupFileDownloadURL(this, s));
		}
		return hm;
	}
	
	
	public HashMap<Integer, Integer> getCurrentProcessingStatus(){
		List<Integer> processes = SystemBackupUtils.getProcessSequence(this);
		HashMap<Integer, Integer> statusMap = new HashMap<Integer, Integer>();
		boolean beforeCurrentProcess = true;
		
		for(int process: processes){
			if(process==getBackupStatus()){
				statusMap.put(process, PROCESS_PROCESSING);
				beforeCurrentProcess = false;
			}else if(beforeCurrentProcess){
				statusMap.put(process, PROCESS_FINISHED_SUCCESSFULLY);
			}else{
				statusMap.put(process, PROCESS_IN_QUEUE);
			}
		}
		
		return statusMap;
	}
	
}
